package com.bt.lms.ui.page.sys.user;

import com.bt.lms.constants.BOConstant;
import com.bt.lms.entity.Role;
import com.bt.lms.entity.User;
import com.bt.lms.entity.page.Page;
import com.bt.lms.ui.page.sys.utils.NetWorkUtils;
import com.bt.lms.ui.vo.PageSkipInfo;
import com.bt.lms.ui.page.common.PageLocation;
import com.bt.lms.main.BotLmsApp;
import com.bt.lms.ui.page.sys.utils.MyButton;
import com.bt.lms.ui.page.sys.utils.MyHBox;
import com.bt.lms.ui.page.sys.utils.MyVBox;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;

import java.time.LocalDate;
import java.util.List;

/**
 * 用户详情界面
 */
public class PageDetails extends VBox {

    /**
     * 要查看详情的用户
     */
    private final User user;

    /**
     * 当前界面的信息
     */
    private final PageSkipInfo pageSkipInfo;

    /**
     * 构建用户详情界面
     *
     * @param user 要查看详情的用户
     * @param pageSkipInfo 当前界面的信息（所在容器 + 所在角标）
     */
    public PageDetails(User user, PageSkipInfo pageSkipInfo) {

        //维护
        this.user = user;
        this.pageSkipInfo = pageSkipInfo;

        //大小
        int wight = BotLmsApp.PER_WIGHT;
        int height = BotLmsApp.HEIGHT;
        this.setPrefWidth(wight);
        this.setPrefHeight(height);

        //布局
        Insets insets = new Insets(0);
        this.setPadding(insets);

        //组件
        //定位区域
        String name = user.getName();
        PageLocation pageLocation = new PageLocation("系统管理", "后台用户管理","查看"+name+"详细信息");
        this.getChildren().add(pageLocation);

        //详情区域
        MyVBox inputVBox = new MyVBox();
        inputVBoxInit(inputVBox);
        this.getChildren().add(inputVBox);

        //关系区域（关联角色）
        MyVBox relationRoleVBox = new MyVBox();
        relationRoleVBoxInit(relationRoleVBox);
        this.getChildren().add(relationRoleVBox);

        //控制区域
        MyHBox controlHBox = new MyHBox();
        controlHBoxInit(controlHBox);
        this.getChildren().add(controlHBox);

    }

    /**
     * 输入区域初始化
     *
     * @param inputVBox 输入区域
     */
    private void inputVBoxInit(MyVBox inputVBox) {

        int padding = 12;
        int spacing = 8;

        //布局
        Insets insets = new Insets(padding);
        inputVBox.setPadding(insets);
        inputVBox.setSpacing(spacing);
        inputVBox.setAlignment(Pos.CENTER_LEFT);

        //组件（说明）
        String nameStr = user.getName();
        String explainStr = nameStr + "的信息";
        Label explainLab = new Label(explainStr);
        //组件添加
        ObservableList<Node> locationChildren = inputVBox.getChildren();
        locationChildren.add(explainLab);

        //组件（位置）
        Label locationLab = new Label("基本信息");
        //组件添加
        ObservableList<Node> explainChildren = inputVBox.getChildren();
        explainChildren.add(locationLab);

        //组件（输入网格
        GridPane gridPane = inputGrid();
        inputVBox.getChildren().add(gridPane);
    }

    /**
     * 网格布局
     *
     * @return 网格布局
     */
    private GridPane inputGrid() {

        //控制数据
        int padding = 5;
        int gap = 15;
        int rom = 0;
        int column;


        //新建网格布局
        GridPane res = new GridPane();
        //大小
        res.setPrefWidth(BotLmsApp.PER_WIGHT);
        res.setPadding(new Insets(padding));
        res.setHgap(gap);//间隙（左右）
        res.setVgap(gap);//间隙（上下）

        //账号部分 eMail
        rom++;
        column = 0;
        Label eMailLab = new Label("账号(电子邮箱)");
        String accountNumberStr = user.getAccountNumber();
        Label accountNumberData = new Label(accountNumberStr);
        res.add(eMailLab, column++, rom);
        res.add(accountNumberData, column, rom);
        GridPane.setHalignment(eMailLab, HPos.RIGHT);

        //用户姓名部分 name
        rom++;
        column = 0;
        Label nameLab = new Label("用户姓名");
        String nameStr = user.getName();
        Label nameDataLab = new Label(nameStr);
        res.add(nameLab, column++, rom);
        res.add(nameDataLab, column, rom);
        GridPane.setHalignment(nameLab, HPos.RIGHT);

        //证件类型部分 idType
        rom++;
        column = 0;
        Label idTypeLab = new Label("证件类型");
        res.add(idTypeLab, column++, rom);
        GridPane.setHalignment(idTypeLab, HPos.RIGHT);
        Integer codeType = user.getCodeType();
        String codeTypeStr  = codeTypeMatch(codeType);
        if (codeTypeStr != null){
            Label idTypeDataLab = new Label(codeTypeStr);
            res.add(idTypeDataLab, column, rom);
        }


        //证件号码部分 id
        rom++;
        column = 0;
        Label codeLab = new Label("证件号码");
        String code = user.getCode();
        Label codeDataLab = new Label(code);
        res.add(codeLab, column++, rom);
        res.add(codeDataLab, column, rom);
        GridPane.setHalignment(codeLab, HPos.RIGHT);

        //初始密码部分 password
        rom++;
        column = 0;
        Label passwordLab = new Label("初始密码");
        Label passwordDataLab = new Label("123456");
        res.add(passwordLab, column++, rom);
        res.add(passwordDataLab, column, rom);
        GridPane.setHalignment(passwordLab, HPos.RIGHT);

        //所属单位部分 unit
        rom++;
        column = 0;
        Label unitLab = new Label("所属单位");
        String unit = user.getUnit();
        Label unitDataLab = new Label(unit);
        res.add(unitLab, column++, rom);
        res.add(unitDataLab, column, rom);
        GridPane.setHalignment(unitLab, HPos.RIGHT);

        //移动电话部分 phone
        rom++;
        column = 0;
        Label phoneLab = new Label("移动电话");
        String mobilePhone = user.getMobilePhone();
        Label phoneDataLab = new Label(mobilePhone);
        res.add(phoneLab, column++, rom);
        res.add(phoneDataLab, column, rom);
        GridPane.setHalignment(phoneLab, HPos.RIGHT);

        return res;
    }

    /**
     * 控制区域初始化
     *
     * @param controlHBox 控制区域
     */
    private void controlHBoxInit(MyHBox controlHBox) {

        //布局
        Insets insets = new Insets(10);
        controlHBox.setPadding(insets);
        controlHBox.setSpacing(10);
        controlHBox.setAlignment(Pos.CENTER);

        //组件(取消)
        MyButton cancelBtn = new MyButton("返回");
        //大小控制
        Insets cancelBtnInsets = new Insets(10, 20, 10, 20);
        cancelBtn.setPadding(cancelBtnInsets);
        //事件
        cancelBtn.setOnAction(event -> NetWorkUtils.reqUserPagingPage(pageSkipInfo,new Page<>()));
        //组件添加
        ObservableList<Node> cancelBtnChildren = controlHBox.getChildren();
        cancelBtnChildren.add(cancelBtn);

    }


    /**
     * 证件类型匹配
     * @param codeType 证件编号
     */
    private String codeTypeMatch(Integer codeType) {

        if (codeType.equals(BOConstant.USER_CODE_TYPE_CARD)){
            return "身份证";

        }else if (codeType.equals(BOConstant.USER_CODE_TYPE_JGZ)){
            return "士官证";

        }else if (codeType.equals(BOConstant.USER_CODE_TYPE_STUDENT_ID)){
            return "学生证";

        }else if (codeType.equals(BOConstant.USER_CODE_TYPE_DRIVERS)){
            return "驾驶证";

        }else if (codeType.equals(BOConstant.USER_CODE_TYPE_PASSPORT)){
            return "护照";

        }else if (codeType.equals(BOConstant.USER_CODE_TYPE_LAISSEZ_PASSER)){
            return "港澳通行证";
        }
        return null;
    }

    /**
     * 关系区域初始化（用户关联的角色）
     *
     * @param relationRoleVBox 关系区域
     */
    private void relationRoleVBoxInit(MyVBox relationRoleVBox) {

        int padding = 12;
        int spacing = 8;
        int tableWight = 490;
        int columnWight = 150;

        //布局
        Insets insets = new Insets(padding,0,padding,0);
        relationRoleVBox.setPadding(insets);
        relationRoleVBox.setSpacing(spacing);

        //说明标签
        Label explainLab = new Label("角色权限");
        this.getChildren().add(explainLab);

        //列表显示
        //列表要显示的数据
        List<Role> roles = user.getRoles();
        ObservableList<Role> rolesTV = FXCollections.observableArrayList(roles);

        //显示列表
        TableView<Role> userTableView = new TableView<>();
        relationRoleVBox.getChildren().add(userTableView);
        //大小
        if (roles.size()>0){
            userTableView.setPrefHeight(roles.size()*40+35);
        }else {
            userTableView.setPrefHeight(70);
        }
        userTableView.setMaxWidth(tableWight);
        //要显示的数据
        userTableView.setItems(rolesTV);

        //列头
        TableColumn<Role, String> codeTC = new TableColumn<>("角色编号");
        userTableView.getColumns().add(codeTC);
        codeTC.setPrefWidth(columnWight);
        TableColumn<Role, String> nameTC = new TableColumn<>("角色名称");
        userTableView.getColumns().add(nameTC);
        nameTC.setPrefWidth(columnWight);
        TableColumn<Role, LocalDate> installRoleTimeTC = new TableColumn<>("设置时间");
        userTableView.getColumns().add(installRoleTimeTC);
        installRoleTimeTC.setPrefWidth(columnWight+30);

        //映射关系
        codeTC.setCellValueFactory(new PropertyValueFactory<>("code"));
        nameTC.setCellValueFactory(new PropertyValueFactory<>("name"));
        installRoleTimeTC.setCellValueFactory(new PropertyValueFactory<>("installRoleTime"));


    }
}
